<template>
	<view class="">
		<!-- <u-empty marginTop="300" iconSize="150" textSize="26" mode="data" /> -->

		<view class="add_box" v-for="(item,index) in UserAddress" :key="index">
			<view class="top">
				<view class="delete" @click="delAddress(item.id)">
					<image class="del_icon" src="../../../static/del_icon.png" mode="widthFix"></image>
				</view>
			</view>
			<view class="scends">
				<view class="scends_text">
					<text style="font-size: 32rpx;font-weight: bold;">{{item.detailed_address}}，{{item.address_door}}</text>
				</view>
				<view class="scends_text">
					{{item.address}}
				</view>
				<view class="scends_text" style="color: #999;">
					{{item.name}} {{item.phone}}
				</view>
			</view>
			<view class="down">
				<!-- <view class="def-box">
					<switch class="def" :checked="item.is_default == 1?true:false" color="#ff723b" style="transform:scale(0.7)" @change="switchChange($event,item.id)" />已设默认{{item.is_default == 1?true:false}}
				</view> -->
				<view class="def-box yes-def-box" v-if="item.is_default == 1">默认</view>
				<view class="def-box" v-else>默认</view>
				<view class="btn-box">
					<view class="mini-btn" @click="copy(item.name,item.phone,item.full_address,item.address_door)">复制</view>
					<view class="mini-btn" @click="edit(item.id)">修改</view>
				</view>


			</view>
		</view>
		<view style="height: 300rpx;width: 100%;">

		</view>
		<view class="but" @click="submit">添加地址</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				UserAddress: []
			}
		},
		onShow() {
			let loginstatus = uni.getStorageSync('token');
			if(loginstatus)
			{
				this.getUserAddress()
			}
			
		},
		methods: {

			getUserAddress() {
				this.$ajaxRequest.get(this.baseUrl + 'shop/Member/UserAddress', {
					uid: uni.getStorageSync('UserInfo').id
				}, true).then(res => {
					if (res.data.code == 200) {
						this.UserAddress = res.data.data
						this.UserAddress.sort(function(a, b) {
							return b.is_default - a.is_default;
						});
						// this.UserAddress.forEach(function(item) {
						//   if(item.is_default == 1){
						// 	  item.status = true
						//   }else{
						// 	  item.status = false
						//   }
						// });
						console.log('this.UserAddress', this.UserAddress);
					} else {
						uni.showToast({
							title: res.data.msg,
							icon: 'none'
						})
					}
				})
			},
			switchChange(e, id) {
				this.$ajaxRequest.post(this.baseUrl + 'shop/Member/defaultAddress', {
					uid: uni.getStorageSync('UserInfo').id,
					id: id,
				}, true).then(res => {
					if (res.data.code == 200) {
						this.getUserAddress()
						console.log('点击');
					} else {
						uni.showToast({
							title: res.data.msg,
							icon: 'none'
						})
					}
				})
			},
			edit(id) {
				uni.navigateTo({
					url: '/pages/yuyue/lijiyuyue/address?type=1&item_id=' + id
				})
			},
			//删除地址
			delAddress(id) {
				let that = this
				uni.showModal({
					title: '提示',
					content: '确认删除收货地址吗？',
					success: function(res) {
						if (res.confirm) {
							that.$ajaxRequest.post(that.baseUrl + 'shop/Member/delAddress', {
								id: id,
							}, true).then(res => {
								if (res.data.code == 200) {
									that.getUserAddress()
								} else {
									uni.showToast({
										title: res.data.msg,
										icon: 'none'
									})
								}
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			copy(name,phone,address,address_door) {
				uni.setClipboardData({
					data: name+'\n'+phone+'\n'+address+" "+address_door, //要被复制的内容
					success: () => { //复制成功的回调函数
						uni.showToast({ //提示
							title: `复制成功`,
							icon: 'success'
						})
					}
				}, true);
			},
			submit() {
				uni.navigateTo({
					url: '/pages/yuyue/lijiyuyue/address?type=0'
				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #FAFAFA;
	}

	.add_box {
		background-color: #fff;
		margin: 0 auto;
		margin-bottom: 20rpx;

		.top {
			padding: 10rpx 1.5% 0 1.5%;
			display: flex;
			justify-content: flex-end;

			.delete {
				width: 35rpx;
				margin-right: 20rpx;

				.del_icon {
					width: 100%;
					height: 100%;
				}
			}
		}

		.scends {
			padding: 0 1.5% 0 1.5%;
			margin-left: 5%;

			.scends_text {
				font-size: 28rpx;
				margin-bottom: 14rpx;
			}
		}

		.down {
			padding: 10rpx 5%;
			width: 100%;
			border-top: 1rpx solid #FAFAFA;
			display: flex;
			justify-content: space-between;
			font-size: 24rpx;

			.def-box {
				padding: 0 6rpx;
				border-radius: 10rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				color: #999;
				.def {}
			}
			.yes-def-box{
				border: 1rpx solid #ff723b;
				color: #ff723b;
			}

			.btn-box {
				width: 50%;
				display: flex;
				justify-content: flex-end;
				align-items: center;

				.mini-btn {
					display: flex;
					justify-content: center;
					align-items: center;
					border-radius: 10rpx;
					border: 1rpx solid #efefef;
					margin: 0 10rpx 0 10rpx;
					width: 100rpx;
					height: 50rpx;
				}
			}
		}
	}

	.but {
		position: fixed;
		bottom: 30rpx;
		width: 690rpx;
		height: 90rpx;
		background: linear-gradient(-17deg, rgba(255, 114, 59, 1), rgba(255, 147, 67, 1));
		border-radius: 45rpx;
		left: 30rpx;
		text-align: center;
		line-height: 90rpx;
		color: white;
	}
</style>
